'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	const { type } = event;
	if(type === 1){//制作简历
		try{
			const {oneList,twoKeyList,twoSkillList,threeList,openid} = event;
			//添加成功
			const writeAdd = await db.collection('projectList').add({
				openid,
				data:{
					oneList,twoKeyList,twoSkillList,threeList
				}
			})
	
			//将返回的id添加到注册的接口中，在首页的时候进行判断
			const signAdd = await db.collection('sign').where({
				openid
			}).update({
				data:{
					id: writeAdd.id
				}
			})
			console.log('注册接口成功',signAdd)
			console.log('提交成功',writeAdd)
			return {
				code: 200,
				status: 'S',
				message:'提交成功'
			}
		}catch(e){
			return{
				code: 2000,
				status: 'N',
				message:'提交失败，请稍后重试'
			}
		}
	}
	if(type === 2){//请求简历数据
		const { openid } = event;
		try{
			const getProjectList = await db.collection('projectList').where({
				openid
			}).get();
			console.log('简历数据加载成功======',getProjectList)
			return{
				code: 200,
				status: 'S',
				data: getProjectList,
				message: '加载成功'
			}
		}catch(e){
			console.log(e);
			return{
				code: 2000,
				status: 'N',
				data: [],
				message: '加载失败'
			}
		}
	}
	//关于我们的数据
	if(type === 3){
		try{
			const abloutList = await db.collection('about').get();
			console.log(abloutList);
			return{
				code: 200,
				status: 'S',
				 data: abloutList,
				 message: '加载成功'
			}
		}catch(e){
			return{
				code: 2000,
				status: 'N',
				 data: '',
				 message: '目前还没有哦~'
			}
		}
	}
	//退出登录
	if( type=== 4){
		try{
			const { openid} = event;
			const loginout = await db.collection('sign').where({
				openid
			}).update({
				data:{
					isLogin: false
				}
			})
			console.log(loginout)
			return{
				code: 200,
				status: 'S',
				 data: loginout,
				 message: '退出登录成功'
			}
		}catch(e){
			return{
				code: 2000,
				status: 'N',
				 data: '',
				 message: '网络开小差啦,请稍后重试！'
			}
		}
	}
	//公告
	if(type === 5){
		try{
			const announcementList = await db.collection('announcement').get();
			console.log(announcementList);
			return{
				code: 200,
				status: 'S',
				 data: announcementList,
				 message: '加载成功'
			}
		}catch(e){
			return{
				code: 2000,
				status: 'N',
				 data: '',
				 message: '网络开小差啦'
			}
		}
	}
	//修改简历
	if(type === 6){
		try{
			const { openid,oneList,twoKeyList,twoSkillList,threeList} = event;
			const updateProject = await db.collection('projectList').where({
				openid
			}).update({
				data:{
					oneList,
					twoKeyList,
					twoSkillList,
					threeList
				}
			})
			console.log('修改简历的数据',updateProject)
			return{
				code: 200,
				status: 'S',
				 data: updateProject,
				 message: '修改成功'
			}
		}catch(e){
			return{
				code: 2000,
				status: 'N',
				 data: '',
				 message: '修改失败'
			}
		}
	}
	
	// 修改密码
	if(type ===7){
		try{
			const { openid, password } = event;
			const updatePassword = await db.collection('sign').where({
				openid
			}).update({
				data:{
					password
				}
			});
			console.log(updatePassword);
			return{
				code: 200,
				status: 'S',
				 data: updatePassword,
				 message: '密码修改成功'
			}
		}catch(e){
			return{
				code: 2000,
				status: 'N',
				 data: '',
				 message: '密码修改失败'
			}
		}
	}
	
	// 获取简历模板的数据
	if( type === 8){
		
		try{
			const { openid } = event;
			const getResume = await db.collection('projectList').where({
				openid
			}).get()
			console.log('获取简历模板云函数成功',getResume)
			return{
				status:'S',
				code: 200,
				data: getResume,
				message: '获取数据成功'
			}
		}catch(e){
			console.log('获取简历模板云函数错误',e)
			return{
				status:'N',
				code: 20000,
				data: [],
				message: '获取数据失败'
			}
		}
	}
};
